<script setup>
import { ref } from 'vue'
import useUserInfoStore from '@/stores/userInfo';
const userInfoStore = useUserInfoStore();
const userInfo = ref({ ...userInfoStore.info })
//定义年龄校验规则
const checkAge = (rule, value, callback) => {
    if(value === ''){
        callback(new Error('请重新输入年龄'))
    }else if(!Number(value)){
        callback(new Error('请输入数字'))
    }else if(Number(value) < 0 || Number(value) > 150){
        callback(new Error('年龄必须是在0 ~ 150的数字'))
    }else{
        callback();
    }
    return
}

//定义校验规则
const rules = {
    nickname: [
        { required: true, message: '请输入用户昵称', trigger: 'blur' },
        {
            pattern: /^\S{1,10}$/,
            message: '昵称必须是1-10位的非空字符串',
            trigger: 'blur'
        }
    ],
    age: [
        { required: true, validator: checkAge, trigger: 'blur'}
    ],
    email: [
        { required: true, message: '请输入用户邮箱', trigger: 'blur' },
        { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
    ],
    phone: [{
        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
        message: "请输入正确的手机号码",
        trigger: "blur"
    }]
}



//修改个人信息
import { userInfoUpdateService } from '@/api/user.js'
import { ElMessage } from 'element-plus';
const updateUserInfo = async () => {
    //调用接口
    if(isNaN(userInfo.value.age)){
        ElMessage.error('年龄非法');
        return;
    }
    if(userInfo.value.phone!=='' && isNaN(userInfo.value.phone)){
        ElMessage.error('手机号非法');
        return;
    }
    let result = await userInfoUpdateService(userInfo.value);
    ElMessage.success(result.msg ? result.msg : '修改成功')
    //修改pinia中的个人信息
    userInfoStore.setInfo(userInfo.value)
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>基本资料</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="userInfo" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="用户名">
                        <el-input v-model="userInfo.username" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="用户昵称" prop="nickname">
                        <el-input v-model="userInfo.nickname"></el-input>
                    </el-form-item>
                    <el-form-item label="用户性别">
                        <el-radio-group v-model="userInfo.sex" style="width: 100%;">
                            <el-radio :value="0">男</el-radio>
                            <el-radio :value="1">女</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="用户年龄" prop="age">
                        <el-input v-model="userInfo.age"></el-input>
                    </el-form-item>
                    <el-form-item label="用户邮箱" prop="email">
                        <el-input v-model="userInfo.email"></el-input>
                    </el-form-item>
                    <el-form-item label="用户手机号" prop="phone">
                        <el-input v-model="userInfo.phone"></el-input>
                    </el-form-item>
                    <el-form-item label="用户地址" prop="address">
                        <el-input v-model="userInfo.address"></el-input>
                    </el-form-item>
                    <el-form-item label="用户爱好" prop="hobby">
                        <el-input v-model="userInfo.hobby" style="width: 240px" :rows="5" type="textarea"
                            placeholder="请输入您的爱好，以便相识志同道合之人" />
                    </el-form-item>
                    <el-form-item label="创建时间" prop="createTime">
                        <el-input v-model="userInfo.createTime" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="修改时间" prop="updateTime">
                        <el-input v-model="userInfo.updateTime" disabled></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updateUserInfo">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>